import dayjs from "dayjs";
import { geQuotationApprovalPageApi } from "@/api/financialManagement/warehouseApprove";
import { computed } from "vue";
import { message } from "ant-design-vue";

export default function OperationHook(state: any) {
  const { searchForm, loading, tableData, drawerOpts, tableRef, dialogOpts } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const getData = async () => {
    const params: any = {
      ...getParams()
    };

    loading.value = true;
    return geQuotationApprovalPageApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
          searchForm.beforePageNum = searchForm.pageNum;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const getParams = () => {
    const { warehouseBillNumList, customerIdList, companyId, channelIdList, state, bizIdList, followIdList, finIdList, depositTime, pageNum, pageSize, beforePageNum } = searchForm;

    const params: any = {
      pageNum,
      pageSize,
      state,
      total: 0,
      beforePageNum,
      warehouseBillNumList: [],
      customerIdList,
      companyId,
      channelIdList,
      bizIdList,
      followIdList,
      finIdList,
      depositTimeStart: depositTime?.length ? dayjs(depositTime[0]).format("YYYY-MM-DD HH:mm:ss") : "",
      depositTimeEnd: depositTime?.length ? dayjs(depositTime[1]).format("YYYY-MM-DD HH:mm:ss") : ""
    };

    if (warehouseBillNumList)
      params.warehouseBillNumList = warehouseBillNumList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");

    if (channelIdList.length) {
      params.channelIdList = channelIdList.map((t: any) => t[t.length - 1]);
    }
    if (searchForm.sort) {
      params.order = searchForm.order;
      params.sort = searchForm.sort;
    }
    return params;
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await getData();
  };

  const batchBillHandle = () => {
    const arr = selectRows.value.find((t: any) => t.state !== 5);
    if (arr) return message.error("运单状态为审批通过才能生成账单!");
    if (!selectRows.value.length) return message.error("请勾选至少一条费用！");
    dialogOpts.title = `生成对账单`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "CreateBill";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  // 审批
  const approvalHandle = (row: SingleOrderApproval.ListRows) => {
    drawerOpts.title = `审批详情`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 2;
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  //查看审批详情

  const showApprovalHandle = (row: SingleOrderApproval.ListRows) => {
    drawerOpts.title = `审批详情`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };
  return {
    searchHandle,
    getData,
    approvalHandle,
    batchBillHandle,
    showApprovalHandle
  };
}
